﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Utilities;
using System.Collections;

namespace StockCommon
{
    public class CalenderHelper
    {

        private Hashtable _NoTradeDateHash = new Hashtable();
        List<NoTradeDate> noTradeDates=new List<NoTradeDate>();

        public static void addNoTrade(DateTime date, String Event)
        {
            StockDataClassesDataContext context = getContext();
            NoTradeDate ndd = (from nd in context.NoTradeDates where nd.日期 == date select nd).FirstOrDefault<NoTradeDate>();
            if (ndd == null)
            {
                ndd = new NoTradeDate();
                ndd.日期 = date;
                ndd.市場 = "台灣";
                ndd.事件 = Event;
                context.NoTradeDates.InsertOnSubmit(ndd);
                context.SubmitChanges();
            }
        }
        public List<NoTradeDate> NoTradeDates
        {
            get { return noTradeDates; }
            set { noTradeDates = value; }
        }
        public void LoadNoTradeDate()
        {
            StockDataClassesDataContext context = getContext();
            List<NoTradeDate> ll=(from d in context.NoTradeDates orderby d.日期 select d).ToList<NoTradeDate>();
            noTradeDates = ll;

            foreach (NoTradeDate d in ll)
            {
                _NoTradeDateHash.Add(d.日期,d.事件);
            }
        }

        public static void makeCalendar(DateTime start, DateTime end)
        {
            if(end.CompareTo(start)<0)
            {
                DateTime tmp=start;
                start=end;
                end=tmp;
            }
            String ss = DateUtils.toYYYYMMDD_Slash(start);
            start = DateTime.Parse(ss);
            String se = DateUtils.toYYYYMMDD_Slash(end);
            end = DateTime.Parse(se);

            StockDataClassesDataContext context = getContext();
            String sql=String.Format("delete from TradeCalendar where date >= '{0}' and date <= '{1}'",ss,se);
            context.ExecuteCommand(sql);

            DateTime curDate = start;
            while (curDate.CompareTo(end) <= 0)
            {
                TradeCalendar rec = new TradeCalendar();
                rec.date = curDate;
                rec.Year = curDate.Year;
                int d = curDate.Day;
                int w = DateUtils.GetWeekNumber(curDate);
                rec.week = w;
                int m = curDate.Month;
                rec.Month = m;
                if((w>50)&&(m==1))
                    rec.YearByWeek = rec.Year  - 1;
                else
                    rec.YearByWeek = rec.Year;
                rec.YearWeekKey = rec.YearByWeek * 100 + rec.week;

                if (curDate.DayOfWeek.Equals(DayOfWeek.Saturday)||curDate.DayOfWeek.Equals(DayOfWeek.Sunday))
                {
                    rec.hasTrade = false;
                }else
                    rec.hasTrade = true;
                context.TradeCalendars.InsertOnSubmit(rec);
                curDate=curDate.AddDays(1.0);
            }
            context.SubmitChanges();
        }

        private static StockDataClassesDataContext getContext()
        {
            StockDataClassesDataContext context = LinqAgent.getContext();
            return context;
        }
    }
}
